import 'dart:math';

import 'package:flame/collisions.dart';
import 'package:flame/components.dart';
import 'package:flame/effects.dart';
import 'package:flame/events.dart';
import 'package:flutter/material.dart';

import '../../../core/mixin/liveable.dart';
import '../day002/component.dart';

class GirlComponent extends SpriteAnimationComponent
    with Tappable, Hoverable, Liveable, HasGameRef {
  /// Pixels/帧
  double speed = 500.0;
  late final RectangleHitbox box;
  double counter = 0;
  late Sprite bulletSprite;


  @override
  Future<void> onLoad() async {
    ///初始化血条
    initPaint(lifePoint: 100, lifeColor: Colors.blue);

    List<Sprite> sprites = [];
    for (int i = 1; i <= 10; i++) {
      sprites.add(
          await Sprite.load('girl/girl0${i.toString().padLeft(2, "0")}.png'));
    }
    animation =
        SpriteAnimation.spriteList(sprites, stepTime: 0.15, loop: false);
    box = RectangleHitbox()..debugMode = false;

    animation =
        SpriteAnimation.spriteList(sprites, stepTime: 0.15, loop: false);
    playing = false;

    bulletSprite = await gameRef.loadSprite('monster.png');
    animation?.onComplete = () {
      animation!.currentIndex = 0;
      animation!.update(0);

      ///添加子弹
      Bullet bullet = Bullet(sprite: bulletSprite, maxRange: 200);
      bullet.size = Vector2(32, 32);
      bullet.anchor = Anchor.center;
      bullet.priority = 1;
      priority = 2;
      bullet.position = position - Vector2(0, -3);
      gameRef.add(bullet);
    };
    add(box);
  }

  @override
  void onGameResize(Vector2 size) {
    super.onGameResize(size);
    position = size / 2;
  }

  ///移动
  void move(Vector2 ds) {
    position.add(ds);
  }

  ///旋转
  void rotateTo(double deg) {
    angle = deg;
  }

  ///翻转
  void flip({bool x = false, bool y = true}) {
    scale = Vector2(scale.x * (y ? -1 : 1), scale.y * (x ? -1 : 1));
  }

  GirlComponent() : super(size: Vector2(150, 150), anchor: Anchor.center);

  @override
  bool onTapDown(TapDownInfo info) {
    box.debugMode = true;
    box.debugColor = Colors.red;
    counter++;
    rotateTo(counter * pi / 2);
    return true;
  }

  @override
  bool onTapUp(TapUpInfo info) {
    box.debugMode = true;
    return true;
  }

  @override
  bool onTapCancel() {
    box.debugMode = false;
    return true;
  }

  @override
  bool onHoverEnter(PointerHoverInfo info) {
    box.debugMode = true;
    box.debugColor = Colors.greenAccent;
    return true;
  }

  @override
  bool onHoverLeave(PointerHoverInfo info) {
    box.debugMode = false;
    return true;
  }

  void toTarget(Vector2 target) {
    ///移除之前移动，防止对下次的移动产生影响
    removeAll(children.whereType<MoveEffect>());
    double timeMs = (target - position).length / speed;
    add(MoveEffect.to(target, EffectController(duration: timeMs)));
  }

  void shoot() {
    playing = true;
    animation?.reset();
  }
}

class Monster extends SpriteAnimationComponent
    with Liveable, Tappable, Hoverable {
  Monster(
    SpriteAnimation animation,
    Vector2 size,
    Vector2 position,
  ) : super(
          animation: animation,
          size: size,
          position: position,
          anchor: Anchor.center,
        );

  @override
  Future<void> onLoad() async {
    ///初始化血条
    initPaint(lifePoint: 1000);
    add(RectangleHitbox()..debugMode = true);
  }

  @override
  bool onTapDown(TapDownInfo info) {
    loss(10);
    return true;
  }

  @override
  void onDied() {
    removeFromParent();
    super.onDied();
  }
}

class DamageText extends PositionComponent {
  final TextStyle damageTextStyle = TextStyle(
    fontSize: 14,
    color: Colors.white,
    fontFamily: 'Menlo',
    shadows: [Shadow(color: Colors.red, offset: Offset(1, 1), blurRadius: 1)],
  );
  final TextStyle critDamageTextStyle = TextStyle(
    fontSize: 14,
    color: Colors.white,
    fontFamily: 'Menlo',
    shadows: [
      Shadow(color: Colors.orange, offset: Offset(2, 2), blurRadius: 3)
    ],
  );

  void addDamage(int damage, {bool isCrit = false}) {
    Vector2 offset = Vector2(-30, 0);
    if (children.isNotEmpty) {
      final PositionComponent last;
      if (children.last is PositionComponent) {
        last = children.last as PositionComponent;
        offset = last.position + Vector2(5, last.height);
      }
    }
    if (!isCrit) {
      addWhiteDamage(damage, offset);
    } else {
      addCritDamage(damage, offset);
    }
  }

  Future<void> addWhiteDamage(int damage, Vector2 offset) async {
    TextComponent damageText =
        TextComponent(textRenderer: TextPaint(style: damageTextStyle));
    damageText.text = damage.toString();
    damageText.position = offset;
    add(damageText); // tag1
    await Future.delayed(const Duration(seconds: 1));
    damageText.removeFromParent();
  }

  Future<void> addCritDamage(int damage, Vector2 offset) async {
    TextComponent damageText =
        TextComponent(textRenderer: TextPaint(style: critDamageTextStyle));
    damageText.text = damage.toString();
    damageText.position = offset;
    TextStyle style = critDamageTextStyle.copyWith(fontSize: 10);
    TextComponent infoText =
        TextComponent(textRenderer: TextPaint(style: style));
    infoText.text = '暴击';
    infoText.position = Vector2(
        offset.x + damageText.width - infoText.width / 2,
        offset.y - infoText.height / 2);
    add(infoText);
    add(damageText);
    await Future.delayed(const Duration(seconds: 1));
    infoText.removeFromParent();
    damageText.removeFromParent();
  }
}
// class Monster extends SpriteComponent {
//   Monster(
//       Sprite sprite,
//       Vector2 size,
//       Vector2 position,
//       ) : super(
//       sprite: sprite,
//       size: size,
//       position: position,
//       anchor: Anchor.center);
//
//   @override
//   Future<void> onLoad() async {
//     add(RectangleHitbox()..debugMode = true);
//   }
// }
